import Vue from 'vue'
import VueRouter from 'vue-router'
import * as apis from '../pages/api/home'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch(err => err) }

const routes = [
  {
    path: "/",
    redirect: "Home",
    meta: {
      nowIndex: 0,
    },
  },
  // 登录首页
  {
    path: "/home",
    name: "Home",
    meta: {
      title: "听故事",
      icon: "iconfont icon-erji1",
      nowIndex: 0,
      flag: true,

    },
    component: () => import("@/pages/listenstory/home_notregister/wei-login.vue")
  },
  // 树洞
  {
    path: "/list",
    name: "list",
    meta: {
      icon: "../../../static/img/图片 6@1x (1).png",
      nowIndex: 1,
      flag: true,
      flags: true,
      daohang: true,
      noList: true

    },
    component: () => import("@/pages/tree/downTalk.vue")
  },
  // 我的未登录
  {
    path: "/my",
    name: "my",

    meta: {
      title: "我的",
      icon: "iconfont icon-wode1",
      nowIndex: 2,
      flag: true,
    },
    component: () => import("@/pages/my/nologin/index.vue"),
    // beforeEnter:(to,from,next)=>{
    //   if(to.path=="/list"){
    //     next(false)
    //   }else{
    //     next()
    //   }
    // }
  },
  {
    path: "/weiTan",
    name: "weiTan",
    meta: {

    },
    component: () => import("@/pages/listenstory/home_notregister/noTan.vue")
  },
  {
    path: "/search",
    name: "search",
    meta: {

    },
    component: () => import("@/pages/listenstory/home_notregister/search.vue")
  },
  // 软文详情
  {
    path: "/rwxq",
    name: "rwxq",
    meta: {
      daohang: true

    },
    component: () => import("@/pages/listenstory/home_register/rwxq.vue")
  },
// 拍照
  {
    path: "/picture",
    name: "picture",
    meta: {
      // daohang: true

    },
    component: () => import("@/pages/listenstory/home_notregister/picture.vue")
  },
  // 登录注册
  {
    path: "/login",
    name: 'login',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_notregister/pipeiLogin.vue")
  },
  {
    path: "/vipWarn",
    name: 'vipWarn',
    meta: {
    },
    component: () => import("@/pages/listenstory/home_register/vipWarn.vue")
  },

  {
    path: "/becomeMember",
    name: 'becomeMember',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/becomeMember.vue")
  },
  {
    path: "/gsjs/:id",
    name: 'gsjs',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/gsjs.vue")
  },
  // 故事详情
  {
    path: "/story/:id",
    name: 'story',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/index.vue")
  },
  {
    path: "/pinglun/:id",
    name: 'pinglun',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/pinglun.vue")
  },
  {
    path: "/share",
    name: 'share',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/share.vue")
  },
  {
    path: "/tgs",
    name: 'tgs',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/listenstory/home_register/story/tgs.vue")
  },


  {
    path: "/babyType",
    name: 'babyType',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/babyType.vue")
  },
  {
    path: "/free",
    name: 'free',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/free.vue")
  },
  {
    path: "/giveFriend",
    name: 'giveFriend',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/giveFriend.vue")
  },
  {
    path: "/giveFriendss",
    name: 'giveFriendss',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/giveFriendzeng.vue")
  },
  {
    path: "/loginIndex",
    name: 'loginIndex',
    meta: {
      daohang: true
    },
    component: () => import("@/pages/my/login/index.vue")
  },
  {
    path: "/inviteFriend",
    name: 'inviteFriend',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/inviteFriend.vue")
  },
  {
    path: "/inviteFriend",
    name: 'inviteFriend',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/inviteFriend.vue")
  },
  {
    path: "/linekefu",
    name: 'linekefu',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/linekefu.vue")
  },
  {
    path: "/MacoAge",
    name: 'MacoAge',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/MacoAge.vue")
  },
  {
    path: "/payMacoAge",
    name: 'payMacoAge',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/payMacoAge.vue")
  }, {
    path: "/weChat",
    name: 'weChat',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/weChat.vue")
  }, {
    path: "/weChatcopy",
    name: 'weChatcopy',
    // path: "/changeMy",
    // name: 'changeMy',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/weChatcopy.vue")
  },{
    path: "/weChatcopyziji",
    name: 'weChatcopyziji',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/weChatcopyziji.vue")
  },
  {
    path: "/mybuy",
    name: 'mybuy',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/mybuy.vue"),
    redirect: '/mybuy/one',
    children: [
      {
        path: "/mybuy/one",
        name: '/mybuy/one',
        meta: {
          daohang: true,
          secondNav: true,
          title: "购买故事",
          // nowIndex:0
          index: 0
        },
        component: () => import("@/pages/my/login/myStoryone.vue")
      },
      {
        path: "/mybuy/two",
        name: '/mybuy/two',
        meta: {
          daohang: true,
          secondNav: true,
          title: "购买会员",
          index: 1
        },
        component: () => import("@/pages/my/login/myStorytwo.vue")
      },
    ]
  },
  {
    path: "/myStory",
    name: 'myStory',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/login/myStory.vue"),
  },
  // {
  //   path: "/nologinIndex",
  //   name:'nologinIndex',
  //   meta: {
  //   },
  //   component: () => import("@/pages/my/nologin/index.vue")
  // },
  {
    path: "/zxkf",
    name: 'zxkf',
    meta: {
      daohang: true

    },
    component: () => import("@/pages/my/nologin/zxkf.vue")
  },
    {
   
        path: "/changeMy",
        name: 'changeMy',
        meta: {
          daohang: true
    
        },
        component: () => import("@/pages/my/nologin/changeMy.vue")
      }, 
  {
    path: "/muyinovip",
    name: 'muyinovip',
    meta: {
    },
    component: () => import("@/pages/my/my-yi-novip.vue")
  }, 
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {

  if (to.meta.noList) {
    if (from.name == "my") {
      next(false)
    } else {
      next()
    }
  } else if (to.meta.daohang) {
    let red= localStorage.getItem("username")
    if(red){
      apis.userDataren({ren:red}).then((res)=>{
        console.log(res.data.code[0].token)
        let tokens =  res.data.code[0].token
        localStorage.setItem("listToken", tokens)
      })  
    }
    if (!localStorage.getItem("userToken") && !localStorage.getItem("username") && !localStorage.getItem('phone')){
      next('/weiTan')
    } else {
      next()
    }
  } else {
    next()
  }
  // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIzMDIiLCJ1bmFtZSI6Iuacsea1qealpiJ9.6t3reuqLbZJ5lFVCVVNpEpMM_jNkB3o_iIcTGO7XnI0
  // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIzMDIiLCJ1bmFtZSI6Iuacsea1qealpiJ9.6t3reuqLbZJ5lFVCVVNpEpMM_jNkB3o_iIcTGO7XnI0




})
export default router
